const BASE_URl_TEST = import.meta.env.VITE_BASE_PATH


import axios from "axios";

export const getRolesPage = async () => {

    try {
        const res = await axios.get(BASE_URl_TEST + '/roles/page', {
            params: {
                pageNum: 1,
                pageSize: 1000000
            }
        })
        return res.data
    } catch (error) {
        console.log('error');
    }

}

export const dengru = async () => {
    const newuser = {
        username: 'admin',
        password: '123456'
    }
    const requestOptions = {
        method: 'post',
        url: 'http://goapi.sanjitian.cn/api/auth/login',
        data: newuser
    }
    try {
        const res = await axios(requestOptions);

        console.log(res);

        if (res.status === 200) { // 或者使用 res.ok 检查响应是否成功
            return res.data; // 直接返回响应数据
        }
    } catch (error) {
        console.error('Error in postRoles:', error);
        // 这里可以处理错误，例如重新抛出错误或者返回错误信息
        throw error; // 或者 return error.response?.data; 来返回具体的错误信息
    }
}

export const postRoles = async () => {
    // 设置请求携带的数据格式是 JSON格式
    const newRole = {
        id: 79896,
        name: '崔梦娇爱吃💩79896',
        code: '79896',
        sort: 2,
        status: 1,
        dataScope: 3
    }
    // axios 配置项
    const requestOptions = {
        method: 'post',
        url: 'http://goapi.sanjitian.cn/api/roles',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJSW5mbyI6eyJ1c2VySWQiOjIsInVzZXJuYW1lIjoiYWRtaW4iLCJuaWNrbmFtZSI6Iuezu-e7n-euoeeQhuWRmGFkbWluIiwiZGVwdElkIjoxLCJyb2xlcyI6IkFETUlOIn0sImlzcyI6ImR5czYyMzAiLCJleHAiOjE3Mjg5MDk5MTgsIm5iZiI6MTcyODg3MzkyOCwiaWF0IjoxNzI4ODczOTE4fQ.9xhdL57Qs38Mlm0K-TCwgrQjET1ZS2jUZhIOob0-pGk'
        },
        data: newRole
    };
    try {
        const res = await axios(requestOptions);

        console.log(res);

        if (res.status === 200) { // 或者使用 res.ok 检查响应是否成功
            return res.data; // 直接返回响应数据
        }
    } catch (error) {
        console.error('Error in postRoles:', error);
        // 这里可以处理错误，例如重新抛出错误或者返回错误信息
        throw error; // 或者 return error.response?.data; 来返回具体的错误信息
    }
}

export const putRolesByRoleID = () => {

    const raw = JSON.stringify({
        "name": "重四易应标压3",
        "code": "8888",
        "sort": 1,
        "status": 1,
        "dataScope": 1
    });

    const requestOptions = {
        method: 'put',
        url: 'http://goapi.sanjitian.cn/api/roles',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJSW5mbyI6eyJ1c2VySWQiOjIsInVzZXJuYW1lIjoiYWRtaW4iLCJuaWNrbmFtZSI6Iuezu-e7n-euoeeQhuWRmGFkbWluIiwiZGVwdElkIjoxLCJyb2xlcyI6IkFETUlOIn0sImlzcyI6ImR5czYyMzAiLCJleHAiOjE3Mjg5MDk5MTgsIm5iZiI6MTcyODg3MzkyOCwiaWF0IjoxNzI4ODczOTE4fQ.9xhdL57Qs38Mlm0K-TCwgrQjET1ZS2jUZhIOob0-pGk'
        },
        data: raw
    };

    axios(BASE_URl_TEST + "/roles/200", requestOptions)
        .then(response => response.data)
        .then(result => console.log(result))
        .catch(error => console.log('error', error));
}


export const deleteRolesByids = () => {

    const requestOptions = {
        method: 'DELETE'
    };

    axios(BASE_URl_TEST + "/roles/878999", requestOptions)
        .then(response => response.data)
        .then(result => console.log(result))
        .catch(error => console.log('error', error));
}


export const kuangbaoshanchu = async () => {

    try {
        const res = await axios.get(BASE_URl_TEST + '/roles/page', {
            params: {
                pageNum: 1,
                pageSize: 100000
            }
        })

        const requestOptions = {
            method: 'DELETE'
        };

        for (const item of res.data.data.list) {
            axios(BASE_URl_TEST + `/roles/${item.id}`, requestOptions)
                .then(response => response.data)
                .then(result => console.log(result))
                .catch(error => console.log('error', error));
        }


    } catch (error) {
        console.log('error');
    }

}

export const kuangbaotianjia = async () => {
    const requests = [];

    for (let a = 50000; a <= 100000; a++) {
        const newRole = {
            id: a,
            name: `廖天帝正在和Cui抢💩吃${a}`,
            code: `${a}`,
            sort: 2,
            status: 1,
            dataScope: 3
        };

        const requestOptions = {
            method: 'post',
            url: 'http://goapi.sanjitian.cn/api/roles', // 确保URL是正确的
            headers: {
                'Content-Type': 'application/json',
                'Authorization': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJSW5mbyI6eyJ1c2VySWQiOjIsInVzZXJuYW1lIjoiYWRtaW4iLCJuaWNrbmFtZSI6Iuezu-e7n-euoeeQhuWRmGFkbWluIiwiZGVwdElkIjoxLCJyb2xlcyI6IkFETUlOIn0sImlzcyI6ImR5czYyMzAiLCJleHAiOjE3Mjg5MDk5MTgsIm5iZiI6MTcyODg3MzkyOCwiaWF0IjoxNzI4ODczOTE4fQ.9xhdL57Qs38Mlm0K-TCwgrQjET1ZS2jUZhIOob0-pGk'
            },
            data: newRole
        };

        requests.push(axios(requestOptions));
    }

    try {
        const results = await Promise.all(requests);
        console.log(results);
        return results; // 返回所有请求的结果
    } catch (error) {
        console.error('Error in kuangbaotianjia:', error);
        throw error; // 或者 return error.response?.data; 来返回具体的错误信息
    }
}

export const kuangbaoxiugai = async () => {

}